package org.wmq.skyserver.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.lettuce.core.output.StatusOutput;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wmq.skycommon.constant.StatusConstant;
import org.wmq.skycommon.exception.DeleteNotAllowedException;
import org.wmq.skycommon.pojo.DTO.DishPageDTO;
import org.wmq.skycommon.pojo.VO.DishVO;
import org.wmq.skycommon.pojo.entity.Dish;
import org.wmq.skycommon.pojo.DTO.DishDTO;
import org.wmq.skycommon.pojo.entity.DishFlavor;
import org.wmq.skycommon.pojo.entity.PageResult;
import org.wmq.skyserver.mapper.DishFlavorMapper;
import org.wmq.skyserver.mapper.DishMapper;
import org.wmq.skyserver.mapper.SetmealDishMapper;
import org.wmq.skyserver.service.DishService;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    @CachePut(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        this.dishMapper.insert(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0) {
            for (DishFlavor f : flavors) {
                f.setId(id);
            }
            this.dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public PageResult pageQuery(DishPageDTO dishPageDTO){
        PageHelper.startPage(dishPageDTO.getPage(),dishPageDTO.getPageSize());
        Page<DishVO> result = this.dishMapper.pageQuery(dishPageDTO);
        return new PageResult(result.getTotal(),result.getResult());
    }

    @Override
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public void deleteBatch(List<Long> ids) throws DeleteNotAllowedException {
        for (Long id : ids){
            Dish dish = this.dishMapper.getById(id);
            if (dish.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeleteNotAllowedException();
            }
        }
        List<Long> setmealIds = this.setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null && setmealIds.size() > 0){
            throw new DeleteNotAllowedException();
        }
        for (Long id : ids) {
            this.dishMapper.deleteById(id);
            this.dishFlavorMapper.deleteByDishId(id);
        }
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish = this.dishMapper.getById(id);
        List<DishFlavor> list = this.dishFlavorMapper.getByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Override
    @CacheEvict(cacheNames = "dishCache", key = "#dishDTO.categoryId")
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        this.dishMapper.update(dish);
        this.dishFlavorMapper.deleteByDishId(dish.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors == null || flavors.size() == 0) {
            return;
        }
        for (DishFlavor f : flavors) {
            f.setId(dish.getId());
        }
        this.dishFlavorMapper.insertBatch(flavors);
    }

    @Override
    @Cacheable(cacheNames = "dishCache", key = "#categoryId")
    public List<Dish> list(Long categoryId) {
        String key = "dish:" + categoryId;
        List<Dish> list = (List<Dish>) this.redisTemplate.opsForValue().get(key);
        if (list == null || list.size() == 0) {
            list = this.dishMapper.listByCategoryId(categoryId);
            this.redisTemplate.opsForValue().set(key,list);
        }
        return list;
    }

}
